using System.Threading.Tasks;
using SharedKernel.Domain.Events;

namespace SharedKernel.Infrastructure.EventBus
{
    /// <summary>
    /// 事件总线接口
    /// </summary>
    public interface IEventBus
    {
        /// <summary>
        /// 发布事件
        /// </summary>
        /// <param name="event">领域事件</param>
        /// <typeparam name="TEvent">事件类型</typeparam>
        Task PublishAsync<TEvent>(TEvent @event) where TEvent : DomainEvent;

        /// <summary>
        /// 订阅事件
        /// </summary>
        /// <typeparam name="TEvent">事件类型</typeparam>
        /// <typeparam name="THandler">事件处理器类型</typeparam>
        void Subscribe<TEvent, THandler>() where TEvent : DomainEvent
            where THandler : IEventHandler<TEvent>;

        /// <summary>
        /// 取消订阅事件
        /// </summary>
        /// <typeparam name="TEvent">事件类型</typeparam>
        /// <typeparam name="THandler">事件处理器类型</typeparam>
        void Unsubscribe<TEvent, THandler>() where TEvent : DomainEvent
            where THandler : IEventHandler<TEvent>;
    }

    /// <summary>
    /// 事件处理器接口
    /// </summary>
    /// <typeparam name="TEvent">事件类型</typeparam>
    public interface IEventHandler<in TEvent> where TEvent : DomainEvent
    {
        /// <summary>
        /// 处理事件
        /// </summary>
        /// <param name="event">领域事件</param>
        Task HandleAsync(TEvent @event);
    }
}